package com.erp.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.dto.LogDTO;
import com.erp.dto.resp.LogRespDTO;
import com.erp.dto.resp.LogStats;
import com.erp.dto.resp.LogUnameStats;
import com.erp.dto.resp.LogUrlStats;
import com.erp.mapper.LogMapper;
import com.erp.pojo.Log;
import com.erp.service.LogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author honor
* @description 针对表【t_log】的数据库操作Service实现
* @createDate 2025-07-04 08:44:36
*/
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log>
    implements LogService {
    @Autowired
    private LogMapper logMapper;

    @Override
    public Map<String, Object> queryLogListService(Integer pageNum, Integer pageSize, LogDTO logDTO) {
//指定参数
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
//查询数据库


        if (logDTO.getId() != null && !("".equals(logDTO.getId()))) {
            List<Integer> ids = Arrays.stream(logDTO.getId().split(",")).map(Integer::parseInt).collect(Collectors.toList());
            logDTO.setIds(ids);
        }

        List<Log> logList = logMapper.queryLogListMapper(logDTO);
        List<LogRespDTO> dtoList = logList.stream().map(l -> {
            LogRespDTO dto = new LogRespDTO();
            BeanUtils.copyProperties(l, dto);
            Date createTime = l.getCreatTime();
            String createTimeStr = DateUtil.format(createTime, "yyyy-MM-dd HH:mm:ss");
            dto.setCreateTimeStr(createTimeStr);
            return dto;
        }).collect(Collectors.toList());


        Map<String, Object> result = new HashMap<>();
        result.put("logList", dtoList);
        result.put("total", page.getTotal());
        return result;
    }

    @Override
    public Map<String, Object> queryLogListServiceV2(Integer pageNum, Integer pageSize, LogDTO log) {
        QueryWrapper<Log> wrapper = new QueryWrapper<Log>()
            .like(log.getUname() != null && !"".equals(log.getUname()), "uname", log.getUname())
            .in(log.getIds() != null && log.getIds().size() > 0, "id", log.getIds())
            .ge(log.getStartTime() != null && !"".equals(log.getStartTime()), "creatTime", log.getStartTime())
            .le(log.getEndTime() != null && !"".equals(log.getEndTime()), "creatTime", log.getEndTime())
            .eq(log.getUid() != null, "uid", log.getUid());

        IPage<Log> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<Log>(log.getPageNum(), log.getPageSize());
        List<Log> list = list(page, wrapper);

        List<LogRespDTO> dtoList = list.stream().map(l -> {
            LogRespDTO dto = new LogRespDTO();
            BeanUtils.copyProperties(l, dto);
            Date createTime = l.getCreatTime();
            String createTimeStr = DateUtil.format(createTime, "yyyy-MM-dd HH:mm:ss");
            dto.setCreateTimeStr(createTimeStr);
            return dto;
        }).collect(Collectors.toList());

        Map<String, Object> result = new HashMap<>();
        result.put("logList", dtoList);
        result.put("total", page.getTotal());
        return result;
    }

    @Override
    public LogStats queryLogListStats() {
        List<Map<String, Object>> list = logMapper.queryLogListStats();
        List<String> xdata = new ArrayList<>();
        List<Long> ydata = new ArrayList<>();

        LogStats logStats = new LogStats();

        for (Map<String, Object> map : list) {
            xdata.add(map.get("date").toString());
            ydata.add((Long) map.get("count"));
        }
        logStats.setXdata(xdata);
        logStats.setYdata(ydata);
        return logStats;
    }

    @Override
    public LogUrlStats queryLogListUrlStats() {
        List<Map<String, Object>> list = logMapper.queryLogListUrlStats();

        List<String> xdata = new ArrayList<>();
        List<Long> ydata = new ArrayList<>();

        LogUrlStats logUrlStats = new LogUrlStats();

        for (Map<String, Object> map : list) {
            xdata.add(map.get("url").toString());
            ydata.add((Long) map.get("count"));
        }

        logUrlStats.setXdata(xdata);
        logUrlStats.setYdata(ydata);


        return logUrlStats;
    }

    @Override
    public LogUnameStats queryLogListUnameStats() {
        List<Map<String, Object>> list = logMapper.queryLogListUnameStats();
        List<String> xdata = new ArrayList<>();
        List<Long> ydata = new ArrayList<>();
        LogUnameStats logUnameStats = new LogUnameStats();

        for (Map<String, Object> map : list) {
            String uname = (String) map.get("uname");
            if (uname == null) {
                uname = "匿名用户";
            }
            xdata.add(uname);
            ydata.add((Long) map.get("count"));
            logUnameStats.setXdata(xdata);
            logUnameStats.setYdata(ydata);

        }
        return logUnameStats;

        }
}



